let express = require('express');
let router = express.Router();

const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

// 实体类
const Response = require('../entity/http/Response');
const ObjId = require('../entity/common/ObjId');
const Resume = require('../entity/job/Resume');

// 业务层
const jobService = require('../service/jobService');

/*
 * 懒加载获取推荐职位接口
 */
router.post('/getAllJob', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let lastJob = new ObjId(req.body, 'lastJobId', true);
    let flag = lastJob.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.getAllJob(uid, lastJob.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取推荐职位失败"));
    });
});

/*
 * 获取职位详情
 */
router.get('/getJobDetail', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let job = new ObjId(req.query, 'jobId');
    let flag = job.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.getJobDetail(uid, job.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取职位详情失败"));
    });
});

// /*
//  * 查询是否收藏岗位接口
//  */
// router.post('/isCollectJob', async function(req, res, next) {
//     let { uid } = req.jwtResult.data;
//     let jobId = req.body.jobId;
//     if (!jobId) {
//         res.status(400).json({ msg: '参数错误' });
//         return;
//     }
//     let result = await jobService.isCollectJob(uid, jobId);
//     res.status(result.success ? 200 : 400).json(result);
// });

/*
 * 职位收藏接口
 */
router.post('/collectJob', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let job = new ObjId(req.body, 'jobId');
    let flag = job.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.collectJob(uid, job.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("职位收藏失败"));
    });
});

/*
 * 取消收藏接口
 */
router.post('/delCollectJob', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let job = new ObjId(req.body, 'jobId');
    let flag = job.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.delCollectJob(uid, job.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("取消收藏失败"));
    });
});

/*
 * 添加个人简历接口
 */
router.post('/addResume', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let resume = new Resume(req);
    let flag = resume.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.addResume(uid, resume).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("添加个人简历失败"));
    });
});

/*
 * 查询个人简历接口
 */
router.get('/getResume', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    jobService.getResume(uid).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("查询个人简历失败"));
    });
});

/*
 * 修改个人简历接口
 */
router.post('/updateResume', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let resume = new Resume(req);
    let flag = resume.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.updateResume(uid, resume).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("修改个人简历失败"));
    });
});

/*
 * 职位投递接口
 */
router.post('/resumeSubmission', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let job = new ObjId(req.body, 'jobId');
    let flag = job.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.resumeSubmission(uid, job.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("职位投递失败"));
    });
});

/*
 * 删除投递记录接口
 */
router.post('/delResumeSubmission', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    let resumeSubmission = new ObjId(req.body, 'resumeSubmissionId');
    let flag = resumeSubmission.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    jobService.delResumeSubmission(uid, resumeSubmission.id).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("删除投递记录失败"));
    });
});

/*
 * 获取所有投递接口
 */
router.get('/getAllResumeSubmission', async function(req, res, next) {
    let { uid } = req.jwtResult.data;
    jobService.getAllResumeSubmission(uid).then((result) => {
        res.status(result.success ? 200 : 400).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("获取所有投递记录失败"));
    });
});

module.exports = router;